<template>
  <div class="loading-body" ref="loadingBody">
    <div>
      <van-loading type="spinner" size="50px" color="#c9c9c9" />
    </div>

  </div>
</template>

<script>
  export default {
    name: 'loading',
    data() {
      return {
        clientHeight: ''
      }
    },
    mounted() {
      this.clientHeight = `${document.documentElement.clientHeight}`
      window.onresize = function temp() {
        this.clientHeight = `${document.documentElement.clientHeight}`
      }
      this.wechatUserInfo()
    },
    watch: {
      // 如果 `clientHeight` 发生改变，这个函数就会运行
      clientHeight: function () {
        this.changeFixed(this.clientHeight)
      }
    },
    methods: {
      changeFixed(clientHeight) {
        this.$refs.loadingBody.style.height = clientHeight + 'px'
      },
      wechatUserInfo() {
        let obj = this.GetRequest()
        this.$get('/api/wechat/userInfo', {code: obj.code}).then((res) => {
          if (res.code === 0) {
            localStorage.setItem('openId', res.data.openId)
            this.userInfo(res.data.accessToken, res.data.openId)
          }
        })
      },
      userInfo(accessToken, openId) {
        this.$get('/api/wechat/getUserInfo', {accessToken: accessToken, openId: openId}).then((res) => {
          if (res.code === 0) {
            localStorage.setItem('headimgurl', res.data.headimgurl)
            localStorage.setItem('nickname', res.data.nickname)
            if (!localStorage.getItem('x-token')) {
              this.$router.push('/login')
            } else {
              this.$router.push('/index')
            }
          }
        })
      }
    }
  }
</script>

<style scoped>
  .loading-body{
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
  }
</style>
